// mircoApp/idShop/integral_My/integral_My.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: {},
    info: [],
    titlePage: "",
    urlstr: "",
    show: false,
    popup_Title: "",
    detailInfo: {},
    page_name: "integral_My",

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.name) {
      this.setData({
        page_name: options.name
      })
    }
    if (options.pool_id) {
      this.setData({
        pool_id: options.pool_id
      })
    }
    this.init();
    if (this.data.page_name == "integral_My") {
      this.setData({
        urlstr: "plugin.project-party.frontend.controllers.index.memberBonusPool",
        titlePage: "我的分润池"
      })
      wx.setNavigationBarTitle({
        title: '我的分润池',
      })
      this.getData();
    } else if (this.data.page_name == "integral_My_Date") {
      this.setData({
        urlstr: "plugin.project-party.frontend.controllers.index.memberBonusPoolDate",
      })
      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  init() {
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0,
      info: [],
      urlstr: ""
    })
  },
  async getData() {
    let _json = {
      page: this.data.page
    };
    if (this.data.page_name == "integral_My_Date") {
      _json.pool_id = this.data.pool_id;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(this.data.urlstr),
      data: _json
    })
    if (data.result) {
      if (this.data.page_name == 'integral_My') {
        this.setData({
          info: data.data.data,
          total_page: data.data.pages,
          isLoadMore: true
        })
      } else {
        this.setData({
          info: data.data.data,
          total_page: data.data.data.pages,
          isLoadMore: true
        })
        console.log(data.data.data)
        if (data.data.data.length > 0) {
          this.setData({
            titlePage: data.data.data[0].dividend_pool_name,
          })
          wx.setNavigationBarTitle({
            title: data.data.data[0].dividend_pool_name,
          })
        }
      }
    } else {
      app.tips(data.msg);
    }
  },
  onclose() {
    this.setData({
      show: false
    })
  },
  tapLink(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (this.data.page_name == 'integral_My') {
      wx.navigateTo({
        url: '/mircoApp/idShop/integral_My/integral_My?name=integral_My_Date&pool_id=' + item.dividend_pool_id,
      })
      return
    } else {
      this.getDateDetail(item);
    }
  },
  async getDateDetail(item) {
    let _json = {
      time: item.time,
      pool_id: item.dividend_pool_id
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.index.memberBonusPoolDetail"),
      data: _json
    })
    if (data.result) {
      this.setData({
        detailInfo: data.data.data,
        popup_Title: item.time + '详情',
        show: true
      })
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let _json = {
        page: this.data.page
      };
      if (this.data.page_name == 'integral_My_Date') {
        _json.pool_id = this.data.pool_id
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss(this.data.urlstr),
        data: _json
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          info: this.data.info.concat(data.data.data)
        })
      } else {
        app.tips(data.msg);
      }
    }

  }
})